package com.future.north.aspect.trace;

import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TraceLogAspect {
    private static final Logger logger = LogManager.getLogger(TraceLogAspect.class);

    @Pointcut("@annotation(com.future.north.aspect.trace.TraceLog)")
    private void pointcut() {
        System.currentTimeMillis();
    }
    @Before(value = "pointcut() && @annotation(traceLog)")
    public void before(JoinPoint joinPoint, TraceLog traceLog) {
        logger.info(JSONObject.toJSONString(joinPoint));
        logger.info(traceLog.key() + "param[" + JSONObject.toJSONString(joinPoint.getArgs()) + "]");
    }

    @After("pointcut() && @annotation(traceLog)")
    private void after(JoinPoint joinPoint, TraceLog traceLog) {
        logger.info("aop时间结束：" + System.currentTimeMillis());
    }
}
